<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/9/27  14:40
 */
namespace app\admin\controller;

class Nav extends Common
{
    private $table_admin='';
    /*
	*	构造函数
	*/
    function __construct(){
        parent::__construct();
    }

    public function lists(){
        $list = db('nav')->paginate();
        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return 	$this->fetch();
    }

    //添加导航页面
    public function add()
    {
        $planPath = APP_PATH.'index/Controller';
        $planList = array();
        $dirRes   = opendir($planPath);
        while($dir = readdir($dirRes))
        {
            if(!in_array($dir,array('.','..','.svn')))
            {
                $planList[] = basename($dir,'.php');
            }
        }
        $this->assign('planList',$planList);

        $id = input('id');
        if($id){
            $data = db('nav')->where(['id'=>$id])->find();
            $this->assign('da', $data);
        }
        if(request()->isAjax()){
            $bool = $this->edit();
            if($bool){
                $this->success('操作成功！', url('Nav/lists'));
            }else{
                $this->success('操作失败！', url('Nav/lists'));
            }
        }
        return $this->fetch();
    }

    //编辑导航
    public function edit()
    {
        $id = input('id');
//        $pid = input('pid');
        $name = input('name');
        $mca = input('controller').'/'.input('action');
        $number = input('number');
        $is_display = input('status');

        $data['name'] = $name;
        $data['mca'] = $mca;
        $data['order_number'] = $number;
        $data['is_display'] = $is_display;

        if(!$id){
            //添加
            $bool = db('nav')->insert($data);
        }else{
            //修改
            $bool = db('nav')->where('id', $id)->update($data);
        }
        return $bool;
    }

    public function del()
    {
        $id = input('id');
        if($id){
            $res = db('nav')->where('id='.$id)->delete();
        }
        if($res){
            $this->success('操作成功', url('Menu/lists'));
        }else{
            $this->success('操作失败', url('Menu/lists'));
        }
    }

    function ajax_get_action()
    {
        $control = input('controller');
        $advContrl = get_class_methods("app\\home\\controller\\".str_replace('.php','',$control));
        $baseContrl = get_class_methods('app\home\controller\Base');
        $diffArray  = array_diff($advContrl,$baseContrl);
        $html = '';
        foreach ($diffArray as $val){
            $html .= "<option value='".$val."'>".$val."</option>";
        }
        exit($html);
    }
}